<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="企业名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'enterpriseName', validatorRules.enterpriseName]" placeholder="请输入企业名称"></a-input>
        </a-form-item>

        <!-- <a-form-item label="乡镇" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <j-dict-select-tag type="list" v-decorator="['fwzId']" :trigger-change="true" dictCode="tb_jrtx_fwz,township_name,id" placeholder="请选择乡镇"/>
         </a-form-item>

         <a-form-item label="自然村" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <j-dict-select-tag type="list" v-decorator="['fwbId']" :trigger-change="true" dictCode="tb_jrtx_fwb,service_name,id" placeholder="请选择自然村"/>
         </a-form-item>-->
        <a-form-item label="乡镇和自然村" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-cascader
            :options="options"
            style="width: 100%"
            @change="onChange"
            :value="cascaderValue"
            placeholder="请输入乡镇和自然村"
          />
        </a-form-item>
        <a-form-item label="申请时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择申请时间" v-decorator="[ 'applicationDate', validatorRules.applicationDate]"
                  :trigger-change="true" style="width: 100%"/>
        </a-form-item>

        <a-form-item label="申请金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-tooltip>
            <template slot="title">
              5万格式：5 &nbsp &nbsp 单位:万元
            </template>
            <a-input style="width:500px" v-decorator="[ 'applicationMoney', validatorRules.applicationMoney]"
                     placeholder="请输入申请金额"></a-input>
          </a-tooltip>
        </a-form-item>
        <!-- <a-form-item label="1_企业申请，2_县中心受理，3_保前受理，4_贷款及担保审核，5_签约及贷款发放" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <j-dict-select-tag type="list" v-decorator="['currentStatus']" :trigger-change="true" dictCode="current_status_qy" placeholder="请选择1_企业申请，2_县中心受理，3_保前受理，4_贷款及担保审核，5_签约及贷款发放"/>
         </a-form-item>-->

        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['state']" :trigger-change="true" dictCode="state"
                             placeholder="请选择状态"/>
        </a-form-item>


      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

    import {httpAction, getAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDate from '@/components/jeecg/JDate'
    import JDictSelectTag from "@/components/dict/JDictSelectTag"

    export default {
        name: "CorporateLoansModal",
        components: {
            JDate,
            JDictSelectTag,
        },
        data() {
            return {
                cascaderValue: [],
                defaultValue: [],
                options: [],
                form: this.$form.createForm(this),
                title: "操作",
                width: 800,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },

                confirmLoading: false,
                validatorRules: {
                    enterpriseName: {rules: [{ required: true, message: '企业名称不能为空'}]},
                    fwzId: {},
                    fwbId: {},
                    applicationDate: {rules: [{ required: true, message: '申请时间不能为空'}]},
                    applicationMoney: {rules: [{ required: true, message: '申请金额不能为空'}]},
                    currentStatus: {},
                    state: {},
                },
                url: {
                    add: "/loanmanagement/corporateLoans/add",
                    edit: "/loanmanagement/corporateLoans/edit",
                }

            }
        },
        created() {
        },
        methods: {
            onChange(value, selectedOptions) {
                if (value.length > 0) {
                    this.cascaderValue = value
                    this.fwbId = selectedOptions[1].code;
                    this.fwzId = selectedOptions[0].code;
                }
            },
            add() {
                this.edit({});
                this.cascaderValue = []
            },
            edit(record) {
                this.fwbId = record.fwbId;
                this.fwzId = record.fwzId;
                const that = this
                getAction("/financialservices/serviceDepartment/serviceDepartMentCascadedDropDown", "").then((res) => {
                    if (res.success) {
                        this.options = res.result
                    }
                    this.loading = false;
                })
                if (JSON.stringify(record) != "{}") {
                    var params = {};
                    params.fwzId = record.fwzId;
                    params.fwbId = record.fwbId;
                    getAction("/financialservices/serviceDepartment/cascadedDropDownEchoResults", params).then((res) => {
                        if (res.success) {
                            that.cascaderValue = res.result
                        }
                        this.loading = false;
                    })
                }
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'enterpriseName', 'fwzId', 'fwbId', 'applicationDate', 'applicationMoney', 'currentStatus', 'state'))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        values.fwbId = this.fwbId;
                        values.fwzId = this.fwzId;
                        if (!values.fwbId) {
                            that.$message.warning("所属自然村不能为空");
                            return ;
                        }
                        if (!values.fwzId) {
                            that.$message.warning("所属乡镇不能为空");
                            return ;
                        }
                        that.confirmLoading = true;
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据", formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }

                })
            },
            handleCancel() {
                this.close()
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row, 'enterpriseName', 'fwzId', 'fwbId', 'applicationDate', 'applicationMoney', 'currentStatus', 'state'))
            }

        }
    }
</script>